var map = new AMap.Map('mapcontainer', {
    resizeEnable: true,
    zoom: 11, //初始地图级别
    center: [121.498586, 31.239637], //初始地图中心点
});
var stompClient
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'xinpai/users/select',
        datatype: "json",
        colModel: [
            { label: '用户ID', name: 'uid', index: 'u_id', width: 50, key: true },
            /*	{ label: '设备imei号', name: 'imei', index: 'imei', width: 80 }, */
            { label: '姓名', name: 'name', index: 'name', width: 80 },
            { label: '所属区域', name: 'areaList', index: 'areaList', width: 80 ,formatter:function (areaList){
                let value="";
                user.areaList.areaList.forEach(function (obj) {
                    areaList.forEach(function (id) {
                        if(id==obj.pid){
                            value=value+obj.projectName+" ";
                        }
                    })
                })
                if(value==""){
                    return "默认区域"
                }else{
                    return value;
                }

            }},
            { label: '所属部门', name: 'deptId', index: 'dept_id', width: 80,formatter:function (value) {
                user.dict.forEach(function (obj) {
                    if(obj.code==parseInt(value)){
                        value=obj.value;
                    }
                })
                return `<span class="label label-success">${value}</span>`;
            }},
            { label: '职位', name: 'positionId', index: 'position_id', width: 80 },
            { label: '手机号', name: 'mobile', index: 'mobile', width: 80 },
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumWidth: 25,
        autowidth:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "list",
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        },
        loadComplete:function (res) {
            if(res.code==0){
                user.areaList.userList=res.list;
                user.$nextTick(function () {
                    $('.collapse').collapse('hide')
                })
                res.list.forEach(function (obj) {
                    if(obj.imei!=""&&obj.imei!=null&&obj.location!=null&&obj.location!=""){
                        let extData={imei:obj.imei}
                        let lng=obj.location.split(",");
                        let lngLat=new AMap.LngLat(lng[0],lng[1]);
                        let marker = new AMap.Marker({
                            icon: "../../statics/img/cleaner.png",
                            position: lngLat,
                            extData:extData,
                            label:{
                                offset: new AMap.Pixel(-20,-30),
                                content: obj.name
                            }
                        });
                        map.add(marker);
                    }
                })

            }
            console.log(map);
            map.setFitView();
        },
        serializeGridData:function (postData) {
            let obj=GetRequest();
            postData.projectId=obj.pid;
            postData.nexus='true';
            return postData;
        },
        beforeSelectRow:function (rowid, e) {
            let list=map.getAllOverlays('marker');
            list.forEach(function (obj) {
                if(obj.getExtData().imei==parseInt(rowid)){
                    map.panTo(obj.getPosition());
                }
            })
        }
    });
});

var user=new Vue({
    el:"#staff_app",
    data:{
        areaList:{areaList:[],userList:[]},
        dict:[]
    },
    methods:{
        selectProjectList:function (data={parentId:0,type:0}) {
            $.ajax({
                type: "GET",
                url:  baseURL + 'xinpai/project/list',
                contentType: "application/json",
                data: data,
                success: function(r){
                    if(r.code === 0){
                        if(data.parentId>0){
                            user.areaList.areaList=r.list;
                        }

                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        selectDictList:function (name) {
            $.ajax({
                type: "GET",
                url:  baseURL + 'sys/dict/select',
                contentType: "application/json",
                data: {name},
                success: function(r){
                    if(r.code === 0){
                        user.dict=r.list;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        }
    },
    created:function() {
        this.selectProjectList({parentId:GetRequest().pid,type:1});
        this.selectDictList("部门");

    }
})
    function connect() {
        const socket = new SockJS('/endpointSang');
        stompClient = Stomp.over(socket);
        stompClient.connect({}, function (frame) {
            let firmId=GetRequest().firmId;
            stompClient.subscribe(`/user/${firmId}/shouhuan`, function (response) {
                let devic =JSON.parse(response.body).user;
                let list=map.getAllOverlays('marker');
                list.forEach(function (obj) {
                    if(obj.getExtData().imei==devic.imei&&devic.state==1){
                        var row=$("#jqGrid").jqGrid('getRowData',devic.imei);
                        row.state=1;
                        $("#jqGrid").jqGrid('getRowData',devic.imei);
                        $("#jqGrid").jqGrid('setRowData',devic.imei,row);
                        if(devic.location!=null){
                            var position=devic.location.split(",");
                            obj.moveTo(new AMap.LngLat(position[0], position[1]),4);
                        }
                    }
                })

            });
        });

    }

function disconnect() {
    if (stompClient != null) {
        stompClient.disconnect();
    }
}
connect();
